<?php namespace Admin\Controller; 
use Hdphp\Controller\Controller;
use Common\Model\TypeAttr;
use Common\Model\GoodsAttr;
/***货品控制器****/
class GoodsListController extends Controller{
	protected $goodsListModel;
	
	public function __init(){
		$this->goodsListModel = new \Common\Model\GoodsList;
	}	
	public function index(){
		if(IS_POST){
			$this->goodsListModel->myadd();
		}
		$gid = Q('get.gid',0,'intval');//获取商品id
		$type_tid = Q('get.tid',0,'intval');//获取商品类型tid
		$typeAttr = new TypeAttr;
		$typeDB = $typeAttr->where("class=1 and type_tid='{$type_tid}'")->get();//获取商品的相关规格
		$goodsAttr = new GoodsAttr;
		foreach ($typeDB as $key => $value) {
			$goodsAttrDB = $goodsAttr->where("taid={$value['taid']}")->get();//获取商品使用的相关规格
			$tmpArr = array();
			foreach ($goodsAttrDB as $gtv) {
				$tmpArr[] = $gtv;
			}
			//$value[$key]['tavalue'] = explode(',', $value['tavalue']);
			$typeDB[$key]['tavalue']=$tmpArr;
		}
		View::with('typeDB',$typeDB);
		
		$listsData = $this->goodsListModel->get();
//		将字段 combine 转换成数组
		foreach ($listsData as $k => $v) {
			$tmpArr=explode(',', $v['combine']);
			foreach ($tmpArr as $key => $value) {
				$tmpArr[$key]=$goodsAttr->where("gtid={$value}")->pluck('gtvalue');
			}
			$listsData[$k]['combine']=$tmpArr;
		}
//		p($listsData);
//		p($typeDB);
		View::with('listsData',$listsData);
		View::make();
	}
	
}